<?php
/*
	Aurus CMS - free OOP CMS
	Copyright (C) 2010  Nestor Yanchuk

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
class AurusForum {
	private $sql = array();
	private $tpl = null;

	public function __construct() {
		$this->tpl = Template::getInstance();
	}
	public function showMain() {
		$this->tpl->load('forum/main');
	//	$this->tpl->set('{BOARD}', );
		$this->tpl->set('{now_time}', au_date(Config::get('timeFormat')));
		return $this->tpl->out();
	}
	public function showCategory($cat = 0) {
		if ($cat) {
			$query = "(type IN('cat','forum')) AND (fid=" . $cat . ' OR parent=' . $cat . ' )';
		} else {
			$query = "type IN('cat','forum')";
		}

		$f_cat = Mysql::getInstance()->select('forum_data', '*', $query);

		$tpl = Template::getInstance();

		$CAT = array();


		for ($i = 0, $n = count($f_cat); $i < $n; $i++) {
			$finfo = $f_cat[$i];
			if ($finfo['type'] == 'forum') {
				$tpl->load('forum/forums');
				$tpl->set('{name}', '<a href="' . Url::formatUrl('forum',array('do' => 'forum', 'viewforum' => $finfo['fid']), 1) . '">' . $finfo['name'] . '</a>');
				$tpl->set('{description}', $finfo['desc']);
				$tpl->set('{topics}', $finfo['topics_c']);
				$tpl->set('{post}', $finfo['posts_c']);
				$tpl->set('{last}', 'last');
				$tpl->set('{status}', $this->getForumStatus($finfo['updated_time']));

				if (!isset($CAT[$finfo['parent']])) {
					$CAT[$finfo['parent']] = '';
				}

				$CAT[$finfo['parent']] .= $tpl->out();
			}
		}

		for ($i = 0, $n = count($f_cat); $i < $n; $i++) {
			$finfo = $f_cat[$i];
			if ($finfo['type'] == 'cat') {
				$tpl->load('forum/category');
				$tpl->set('{category}', '<a href="' . Url::formatUrl('forum',array('do' => 'forum', 'viewcategory' => $finfo['fid']), 1) . '">' . $finfo['name'] . '</a>');
				$tpl->set('{forums}', isset($CAT[$finfo['fid']]) ? $CAT[$finfo['fid']] : '');
				Aurus::getInstance()->addContent( $tpl->out());
			}
		}

		return $this->showMain();
	}
	public function render() {
		switch ($this->getPageToShow()) {
			case 'category':
				return $this->showCategory($_GET['viewcategory']);
				break;
			case 'forum':
				return $this->showForum((int)$_GET['viewforum']);
				break;
			case 'topic':

				break;
			case 'main':
				return $this->showCategory();
				break;
			case 'post':
				return;
				break;
			case 'servise':

				break;

		}
	}
	private function getPageToShow() {
		if (isset($_GET['viewcategory'])) return 'category';
		if (isset($_GET['viewforum'])) return 'forum';
		if (isset($_GET['viewtopic'])) return 'topic';
		if (isset($_GET['viewpost'])) return 'post';
		if (isset($_GET['servise'])) return 'servise';
		return 'main';
	}
	private function getForumStatus($updateTime) {
		$lastLogin = Users::getInstance()->getLoggedUsersData('last_login');
		if ($lastLogin > $updateTime) {
			return '<img src="{THEME}/forum/images/forum_old.gif" alt="' . __('Немає нових повідомленнь') . '"/>';
		} else {
			return '<img src="{THEME}/forum/images/forum_new.gif" alt="' . __('Є нові повідомлення') . '"/>';
		}

	}
	private function showForum($id) {
		$fdata = Mysql::getInstance()->select('forum_data', '*', 'fid=' . $id);
		$tpl = Template::getInstance();
		$fdata = $fdata[0];

		$tpl->load('forum/forum');

		if ($fdata['rules']) {
			$tpl->set('{rules}', $fdata['rules']);
			$tpl->set('[rules]');
			$tpl->set('[/rules]');
		} else {
			$tpl->deleteBlock('rules');
		}

		if (Users::getInstance()->can(Users::getInstance()->getLoggedUsersData('uid'), 'addForumThreads')) {
			$tpl->set('[/new_topic]');
			$tpl->set('[new_topic]');
		} else {
			$tpl->deleteBlock('[/new_topic]');
		}

		$tpl->set('{forum}', $fdata['name']);

		$tpl->set('{forum}', $fdata['name']);
		$tpl->set('{forum}', $fdata['name']);
		return $tpl->out();
	}
}